﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuadRemote.Core;
using System.Drawing;
using System.Xml;

namespace QuadRemote.Tabs
{
    class GraphTab : TabPage
    {
        List<GraphFrame> frames;

        int w = 0;
        int h = 0;

        int size_w;
        int size_h;
        int size_side;
        int size_spacing;

        Button addRight;
        Button removeRight;
        Button addBottom;
        Button removeBottom;
        ListBox saved;
        Button saveGraphs;

        TrackBar realtimeDelay;
        Label lblRealtimeDelay;
        CheckBox sync;

        int[] realtimeLookup = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40, 50, 75, 100, 150, 200, 250, 500, 1000 };

        void setRealtimeValues()
        {
            int ticks = 0;
            if (realtimeDelay.Value > 0)
            {
                ticks = 1000 / realtimeLookup[realtimeDelay.Value];
            }

            byte[] packet = new byte[FlightSession.current().Log.values.Count * 3];

            int pos = 0;
            foreach (GraphFrame f in frames)
            {
                foreach (LogValueGraph g in f.plots)
                {
                    int id = (int)g.container.id;
                    packet[pos++] = (byte)g.container.id;
                    packet[pos++] = (byte)(ticks >> 8);
                    packet[pos++] = (byte)(ticks);
                }
            }

            new CommandOut(CommandId.CMD_LOG_SET_INTERVAL, packet).send();

            Console.WriteLine(Tools.getHexString(packet));
  
            //Bluetooth.instance().sendPriorityPacket(packet);
        }

        void realtimeDelay_ValueChanged(object sender, EventArgs e)
        {
            if (realtimeDelay.Value == 0)
                lblRealtimeDelay.Text = "Off";
            else
                lblRealtimeDelay.Text = realtimeLookup[realtimeDelay.Value] + "hz";

            setRealtimeValues();
        }

        void retrieve_Click(object sender, EventArgs e)
        {
            new CommandOut(CommandId.CMD_LOG_SEND_DEFINITIONS).send();

            //Bluetooth.instance().sendPriorityPacket((byte)Bluetooth.CMD_ID.CMD_LOG_SEND_DEFINITIONS);
        }

        void sync_Click(object sender, EventArgs e)
        {
            
        }


    }
}
